﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myBoard.App.MagnetStore;
using System.Collections.ObjectModel;
using System.Windows.Input;
using myBoard.App.Components.MagnetsStore.Commands;
using System.ComponentModel;
using myBoard.App.Model;

namespace myBoard.App.Components.MagnetsStore.ViewsModels
{
    public class MagnetViewModel : INotifyPropertyChanged
    {
        internal Magnet _magnet;
        string _thumbnailPath;
        string _magnetPath;
        ObservableCollection<string> _screenshotsPath;
        ObservableCollection<CategoryViewModel> _categories;
        ObservableCollection<string> _screenshotsUrl;
        ObservableCollection<MagnetCommentViewModel> _magnetCommentsViewModel;        
        SelectMagnetCommand _selectMagnetCmd;
        DownloadMagnetCommand _downloadMagnetCommand;
        int _downloadProgress;

        public bool CanSubmitReview
        {
            get { return _magnet.CanSubmitReview; }
            set { _magnet.CanSubmitReview = value; RaisePropertyChanged("CanSubmitReview"); }
        }

        public DownloadMagnetCommand DownloadMagnetCommand
        {
            get { return _downloadMagnetCommand; }
            set { _downloadMagnetCommand = value; }
        }

        public SelectMagnetCommand SelectMagnetCmd
        {
            get { return _selectMagnetCmd; }
            set { _selectMagnetCmd = value; }
        }

        public ObservableCollection<MagnetCommentViewModel> MagnetCommentsViewModel
        {
            get { return _magnetCommentsViewModel; }
            set 
            {
                _magnetCommentsViewModel = value;
                RaisePropertyChanged("MagnetCommentsViewModel");
            }
        }

        #region Properties

        public string Name 
        {
            get
            {
                return _magnet.Name;
            }
            set
            {
                _magnet.Name = value;
                RaisePropertyChanged("Name");
            }
        }

        public string StoreID 
        {
            get
            {
                return _magnet.StoreID;
            }
            set
            {
                _magnet.StoreID = value;
                RaisePropertyChanged("StoreID");
            }
        }

        public int? Rating
        {
            get
            {
                return _magnet.Rating;
            }
        }

        public string ShortDesc 
        {
            get
            {
                return _magnet.ShortDesc;
            }
            set
            {
                _magnet.ShortDesc = value;
                RaisePropertyChanged("ShortDesc");
            }
        }

        public string LongDesc 
        {
            get
            {
                return _magnet.LongDesc;
            }
            set
            {
                _magnet.LongDesc = value;
                RaisePropertyChanged("LongDesc");
            }
        }

        public string Version 
        {
            get
            {
                return string.Format("{0}.{1}", _magnet.VersionMajor, _magnet.VersionMinor);
            }
            set
            {
                if (value == null)
                    return;

                if (!value.Contains("."))
                    return;

                string[] version = value.Split('.');

                if (version.Length != 2)
                    return;

                byte major;
                byte minor;
                if (!byte.TryParse(version[0], out major))
                    return;
                if (!byte.TryParse(version[1], out minor))
                    return;

                _magnet.VersionMajor = major;
                _magnet.VersionMinor = minor;
                RaisePropertyChanged("Version");
            }
        }

        public string MagnetPath
        {
            get
            {
                return _magnetPath;
            }
            set
            {
                _magnetPath = value;
                RaisePropertyChanged("MagnetPath");
            }
        }

        public string ThumbnailPath
        {
            get
            {
                return _thumbnailPath;
            }
            set
            {
                _thumbnailPath = value;
                RaisePropertyChanged("ThumbnailPath");
            }
        }

        public ObservableCollection<CategoryViewModel> Categories
        {
            get
            {
                return _categories;
            }
            set
            {
                _categories = value;
                RaisePropertyChanged("Categories");
            }
        }

        public ObservableCollection<string> ScreenshotsPath 
        {
            get
            {
                return _screenshotsPath;
            }
        }        

        public ICommand SubmitCommand { get; set; }

        public string ThumbnailUrl { get; set; }
        
        public ObservableCollection<string> ScreenshotsUrl 
        {
            get
            {
                if (_screenshotsUrl == null)
                {
                    _screenshotsUrl = new ObservableCollection<string>();
                    foreach (Screenshot screenshot in _magnet.Screenshots)
                        _screenshotsUrl.Add(screenshot.Url);
                }
                return _screenshotsUrl;
            }
        }

        public string DownloadUrl
        {
            get
            {
                return _magnet.DownloadUrl;
            }
        }

        public int DownloadProgress 
        {
            get { return _downloadProgress; }
            set
            {
                _downloadProgress = value;
                RaisePropertyChanged("DownloadProgress");
            }
        }

        

        #endregion

        public MagnetViewModel(Magnet magnet)
        {
            _magnet = magnet;
            _selectMagnetCmd = new SelectMagnetCommand();
            _downloadMagnetCommand = new DownloadMagnetCommand();
            ThumbnailUrl = magnet.ThumbnailUrl;
            _screenshotsPath = new ObservableCollection<string>();
            _screenshotsPath.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_screenshotsPath_CollectionChanged);
            SubmitCommand = new SubmitMagnetCommand();
            Instances instances = new Instances();
            _categories = new ObservableCollection<CategoryViewModel>();
            foreach (CategoryViewModel cvm in instances.CategoriesViewModel.AllCategories)
            {
                CategoryViewModel cvmClone = new CategoryViewModel(cvm._category);               

                if (magnet.Categories != null && magnet.Categories.Count > 0)
                {
                    foreach (Category cat in magnet.Categories)
                    {
                        if (cat.Id == cvmClone.Id)
                            cvmClone.IsChecked = true;
                    }
                }               

                _categories.Add(cvmClone);
            }            
            
            _categories.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_categories_CollectionChanged);
        }

        void _categories_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _magnet.Categories = new ObservableCollection<Category>();

            foreach (CategoryViewModel cvm in _categories)
            {
                _magnet.Categories.Add(cvm._category);
            }
        }

        void _screenshotsPath_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RaisePropertyChanged("ScreenshotsUrl");
        }

        internal void SyncModelWithViewModel()
        {
            #region Categories sync

            _magnet.Categories = new ObservableCollection<Category>();
            _magnet.Categories.Clear();
            foreach (CategoryViewModel cvm in _categories)
            {
                if (cvm.IsChecked)
                {
                    _magnet.Categories.Add(cvm._category);
                }
            }

            #endregion
        }


        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
